package evi.com.evi.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import evi.com.evi.R;
import evi.com.evi.application.MyApplication;
import evi.com.evi.login.LoginActivity;
import evi.com.evi.utils.ConnectUtil;
import evi.com.evi.utils.ContentUtils;
import evi.com.evi.utils.JSONUtils;
import evi.com.evi.utils.SPUtil;
import evi.com.evi.utils.ScreenUtils;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by sunyu on 2017/3/30.
 */

public class GuideActivity extends Activity {

    private OkHttpClient client;
    private JSONUtils jsonUtils;
    private String token;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        MyApplication.getInstance().addActivity(this);
        ScreenUtils.setTranStatus(this, true, Color.parseColor("#2387f5"));


        client = new OkHttpClient();
        jsonUtils = new JSONUtils();
        token = SPUtil.getString(this, "token");

        Log.e("TAG", "token = " + token);
        if ("".equals(token)) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            judge();
        }
    }

    private void judge() {

        Request request = new Request.Builder()
                .addHeader("token", token)
                .url(ContentUtils.tokenUser_url)
                .build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
//                toast("网络信号不佳，加载失败");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(ConnectUtil.isConnect(GuideActivity.this)){
                            toast("网络信号不佳，加载失败");
                        } else {
                            toast("网络未联接，请检查");
                        }
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String jsonData = response.body().string();
                Log.e("TAG", "返回数据 = " + jsonData);
                if (jsonUtils.validate(jsonData)) {
                    JSONObject jsonObject = JSON.parseObject(jsonData);
                    if (jsonObject.getBoolean("success")) {

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                Intent intent = new Intent(GuideActivity.this, HomeActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        });

                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
                                startActivity(intent);


                                finish();
                            }
                        });
//                        toast(jsonObject.getString("message"));
                        Log.e("TAG", "message = " + jsonObject.getString("message"));
                    }
                }
            }
        });
    }

    private void toast(final String string) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(GuideActivity.this, string, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.getInstance().deleteActivity(this);
    }
}
